/*
 *  * Copyright (C) 2021 Huawei Device Co., Ltd.
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */

package com.example.smileyrating.slice;

import com.example.smileyrating.ResourceTable;
import com.hsalf.smileyrating.helper.SmileyActiveIndicator;

import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.BaseItemProvider;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.ListContainer;

import java.util.LinkedList;
import java.util.List;

public class ListItemProviderExample extends AbilitySlice {
    private ComponentContainer rootContainer;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        rootContainer = (ComponentContainer) LayoutScatter.getInstance(getContext())
            .parse(ResourceTable.Layout_list_item_provider, null, false);

        super.setUIContent(rootContainer);
    }

    @Override
    public void onActive() {
        super.onActive();
        intializeViews();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    private void intializeViews() {
        ListContainer containerlists = (ListContainer) rootContainer.findComponentById(ResourceTable.Id_recycler_view);
        List<Integer> selection = new LinkedList<>();
        for (int i = 0; i < 30; i++) {
            selection.add(1);
        }

        final SmileyActiveIndicator smileyActiveIndicator = new SmileyActiveIndicator();

        Adapter adapter = new Adapter(this, selection, smileyActiveIndicator);
        containerlists.setItemProvider(adapter);

    }

    public static class Adapter extends BaseItemProvider {
        private ListItemProviderExample listItemProviderExample;

        private List<Integer> mSelection = new LinkedList<>();

        private final SmileyActiveIndicator mSmileyActiveIndicator;

        public Adapter(ListItemProviderExample listItemProviderExample, List<Integer> data,
            SmileyActiveIndicator smileyActiveIndicator) {
            this.listItemProviderExample = listItemProviderExample;
            mSelection.addAll(data);
            mSmileyActiveIndicator = smileyActiveIndicator;
        }

        @Override
        public int getCount() {

            return mSelection.size();
        }

        @Override
        public Object getItem(int i) {
            return mSelection.get(i);
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public Component getComponent(int i, Component component, ComponentContainer componentContainer) {
            Component container = LayoutScatter.getInstance(listItemProviderExample)
                .parse(ResourceTable.Layout_layout_smiley_list_items, null, false);
            return container;
        }

    }
}
